package com.codechuan.hallo.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.codechuan.hallo.dto.CityInfo;
import com.codechuan.hallo.utils.WeChatUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ObtainOrderThread implements Runnable {

    SynContainer synContainer;
    CityInfo cityInfo;
    Integer sleep;

    public ObtainOrderThread(SynContainer synContainer, CityInfo cityInfo, Integer sleep) {
        this.synContainer = synContainer;
        this.cityInfo = cityInfo;
        this.sleep = sleep;
    }

    boolean flag = true;
    @Override
    public void run() {
        while (flag) {
            // 生成订单
            String name = Thread.currentThread().getName();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(name + "开始时间是" + simpleDateFormat.format(new Date()) + "----------------------------------");
            String weChatMiniProgramOrderList = null;
            try {
                weChatMiniProgramOrderList = WeChatUtil.getWeChatMiniProgramOrderList(cityInfo);
            } catch (IOException e) {
                System.out.println("发现刷新过快");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    throw new RuntimeException(ex);
                }
            }
            try {
                JSONObject one = JSONObject.parseObject(weChatMiniProgramOrderList);
                assert one != null;
                JSONArray jsonArray = one.getJSONObject("data").getJSONArray("list");
                for (Object orderObject : jsonArray) {
                    JSONObject jsonObject = JSONObject.parseObject(orderObject.toString());
                    // 判断订单金额
                    Integer price = jsonObject.getInteger("price");
                    // 除于100
                    price = price / 100;
                    if (price <= 100)
                        continue;
                    // 判断匹配规则(人数)
                    Integer passengerCount = jsonObject.getInteger("passengerCount");
                    if (passengerCount < 2)
                        continue;
                    // 判断距离乘客距离
                    // 判断行程总长
                    System.out.println("匹配到的订单是:" + jsonObject.toJSONString());
                }
            } catch (Exception e) {
                try {
                    System.out.println("发现刷新过快");
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    throw new RuntimeException(ex);
                }
            }
            System.out.println(name + "结束时间是" + simpleDateFormat.format(new Date()) + "----------------------------------");
            try {
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

